

const MouseGenerate= (dom = document.body) => {
    return class Mouse{
        static mouseDownList = [];
        static mouseUpList = [];
        static excludeDefaultBehavior = [];
        static pointer = {
            x: 0,y: 0
        }
    
        static move = {
            x: 0,y: 0
        }
    
        static{
            dom.addEventListener("mousedown",(e)=>{
                // 0:left 2:right 1:center 3:next  4: up
                if(Mouse.mouseDownList.indexOf(e.button) > -1) return;
                
                Mouse.mouseDownList.push(e.button);
    
                for (let i = 0; i < Mouse.excludeDefaultBehavior.length; i++) {
                    const fun = Mouse.excludeDefaultBehavior[i];
                    if(fun()){
                        e.preventDefault();
                        break;
                    }
                }
            })
    
            dom.addEventListener("mouseup",(e)=>{
                const index = Mouse.mouseDownList.indexOf(e.button)
                
                if(index>-1){
                    Mouse.mouseDownList.splice(index,1)
                }
    
                Mouse.mouseUpList.push(e.button)
    
                setTimeout(()=>{
                    const index = Mouse.mouseUpList.indexOf(e.button)
                    if(index > -1 ){
                        Mouse.mouseUpList.splice(index,1)
                    }
                },50)
            })
    
            dom.addEventListener("mousemove",(e)=>{
                Mouse.move = {
                    x: e.movementX,
                    y: e.movementY,
                }
    
                Mouse.pointer = {
                    x: e.offsetX,
                    y: e.offsetY,
                }
            })
        }
    
        static isMouseDown(key){
            if(Mouse.mouseDownList.indexOf(key)>-1){
                return true;
            }
    
            return false;
        }
    
        static isMouseUp(key){
            if(Mouse.mouseUpList.indexOf(key)>-1){
                return true;
            }
    
            return false;
        }
    
        static clear(){
            Mouse.mouseUpList.length = 0;
        }
    
        static setExcludeDefaultBehavior( fun ){
            Mouse.excludeDefaultBehavior.push( fun.bind( Mouse ) );
        }
    
        static getPointerRelative($el){
            if($el instanceof HTMLElement){
                const { left, right } = $el.getBoundingClientRect()
                const doc = document.body.getBoundingClientRect()
                
                return {
                    x: Mouse.pointer.x - left,
                    y: Mouse.pointer.y - right,
                }
            }
    
            return Mouse.pointer
        }
    
        isMouseDown(key){
            return Mouse.isMouseDown( key );
        }
    
        isMouseUp(key){
            return Mouse.isMouseUp( key );
        }
    
        clear(){
            Mouse.clear();
        }
    
        setExcludeDefaultBehavior( fun ){
            Mouse.setExcludeDefaultBehavior( fun );
        }
    
        getMove(){
            return Mouse.move;
        }
    
        getPointer(){
            return Mouse.pointer;
        }
    
        getPointerRelative($el){
            return Mouse.getPointerRelative($el)
        }
    
        destory(){
        }
    }
}

const Mouse = MouseGenerate()

Mouse.create = MouseGenerate

export default Mouse